package org.cycuchy.hp.app {
	
	import org.cycuchy.hp.components.book.PageWithBrowser_code;
	import org.cycuchy.hp.data.IDatas;
	
	public class BrowserManager	{
		
		private var page:Number;
		private var pageCount:Number;
		
		public var left:PageWithBrowser_code;
		public var right:PageWithBrowser_code;
		
		private var _datas:IDatas;
		
		public function BrowserManager() {
			page = 0;
		}
		
		public function get datas():IDatas {
			return _datas;
		}
		
		public function set datas(value:IDatas):void {
			_datas = value;
			init();
		}
		
		private function init():void {
			var n:uint = _datas.elementsOnPage * 2;
			pageCount = Math.ceil(_datas.dataLength / n);
			this.left.init(_datas);
			this.right.init(_datas);
		}
		
		public function browseFunction(left:Boolean, fast:Boolean):void {
			if (fast) {
				page = left ? 0 : pageCount - 1;				
			} else {
				if (!left) {
					page++;
				} else {
					page--;
				}
				if (page < 0) {
					page = 0;
				}
			}
			enableBrowserButtons();
			loadContent();
		}
		
//		public function fastBrowseFunction(left:Boolean):void {
//			page = left ? 0 : pageCount - 1;
//			enableBrowserButtons();
//			loadContent();
//		}		
		
		private function enableBrowserButtons():void {
			this.left.browseEnabled = page > 0;
			this.left.fastBrowseEnabled = page > 0;
			this.right.browseEnabled = page < pageCount - 1;
			this.right.fastBrowseEnabled = page < pageCount - 1;
		} 
		
		private function loadContent():void {
			var n:uint = _datas.elementsOnPage; 
			this.left.loadContent(page * 2 * n);
			this.right.loadContent(n * (2 * page + 1));
		}
		
	}
	
}